package net.hhk.twitter;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.hhk.connect.HTTPConnect;
import net.hhk.controller.TweetAdapter;
import net.hhk.controller.TweetManager;
import net.hhk.controller.lazyload.ImageLoader;
import net.hhk.data.Constant;
import net.hhk.data.MySharePreferences;
import net.hhk.data.ThreadedRequest;
import net.hhk.data.Tweet;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.YuvImage;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Spannable;
import android.text.TextPaint;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.text.util.Linkify.MatchFilter;
import android.text.util.Linkify.TransformFilter;
import android.util.Log;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class ViewTweetActivity extends Activity implements OnClickListener {
	private Tweet tweet;
	private int tweetid;
	private String nameRetweet;
	private Boolean isMyRetweet;
	private Boolean isMyFavorite;
	private Boolean currentMyRetweet;
	private Boolean currentMyFavorite;
	private Button rt;
	private Button favor;
	static MySharePreferences sharePre;
	private Boolean isMyTweet;
	private final Context context = this;
	private ArrayList<String> mention_array = new ArrayList<String>();
	private String mention_string = "";
	private TweetAdapter replyAdapter, replyFromAdapter;
	private TweetManager replyManager, replyFromManager;
	private ImageLoader imageLoader;
	private static LayoutInflater inflater = null;
	private LinearLayout lo;
	private ScrollView scroll;
	private Boolean isFirstLoadReply = true;
	private Boolean isError = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle b = getIntent().getExtras();
		sharePre = (MySharePreferences) getApplicationContext();
		tweetid = b.getInt("id");
		nameRetweet = b.getString("retweetUser");
		isMyFavorite = b.getBoolean("isfavorite");
		isMyRetweet = b.getBoolean("isretweet");
		currentMyFavorite = isMyFavorite;
		currentMyRetweet = isMyRetweet;

		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
		replyManager = new TweetManager(getApplicationContext());
		replyFromManager = new TweetManager(getApplicationContext());
		String VisitPerson = sharePre.getExitString("currentVisitUsername");

		String MyUsername = sharePre.getExitString("myUsername");
		if (VisitPerson.equals(MyUsername)) {
			setContentView(R.layout.my_tweet_detail_layout);
			isMyTweet = true;
		} else {
			setContentView(R.layout.tweet_detail_layout);
			isMyTweet = false;
		}

		TextView tweet_title = (TextView) findViewById(R.id.tweet_title);
		tweet_title.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(),
						MainActivity.class);
				i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(i);
				finish();

			}
		});
		scroll = (ScrollView) findViewById(R.id.scroll);

		lo = (LinearLayout) findViewById(R.id.inReplyTo);

		LinearLayout write_tweet = (LinearLayout) findViewById(R.id.home_write_tweet);
		write_tweet.setOnClickListener(clickWriteTweet);

		Button btn = (Button) findViewById(R.id.btn_tweet);
		btn.setOnClickListener(clickWriteTweet);

		LinearLayout searchLayout = (LinearLayout) findViewById(R.id.btnSearch_layout);
		searchLayout.setOnClickListener(clickSearch);
		Button btnSearch = (Button) findViewById(R.id.btn_search);
		btnSearch.setOnClickListener(clickSearch);

		loadDetailTweet();
		showTweet();
		favor = (Button) findViewById(R.id.tweet_dt_favor);
		favor.setOnClickListener(this);
		rt = (Button) findViewById(R.id.tweet_dt_retweet_button);
		if (!isMyTweet) {
			rt.setOnClickListener(this);

		} else {
			Button delete = (Button) findViewById(R.id.tweet_dt_delete);
			delete.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
							context);

					alertDialogBuilder
							.setMessage(
									"Are you sure want to delete this Tweet")
							.setCancelable(false)
							.setPositiveButton("Yes",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											deleteTweet();
										}
									})
							.setNegativeButton("No",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											// if this button is clicked, just
											// close
											// the dialog box and do nothing
											dialog.cancel();
										}
									});

					// create alert dialog
					AlertDialog alertDialog = alertDialogBuilder.create();

					// show it
					alertDialog.show();

				}
			});
		}
		Button reply = (Button) findViewById(R.id.tweet_dt_reply);
		reply.setOnClickListener(this);

	}

	private OnClickListener clickSearch = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i = new Intent(getApplicationContext(), SearchActivity.class);

			startActivity(i);

		}
	};
	private OnClickListener clickWriteTweet = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i = new Intent(getApplicationContext(),
					WriteTweetActivity.class);
			Bundle b = new Bundle();

			b.putInt("isReply", 0);
			i.putExtras(b);
			startActivity(i);

		}
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		String isError = sharePre.getExitString(Constant.ERROR_NOT_FOUND_USER);
		if (isError.equals("true")) {
			String message = sharePre.getExitString("MeassageError");
			int duration = Toast.LENGTH_SHORT;

			Toast toast = Toast.makeText(context, message, duration);
			toast.show();
			sharePre.setExitString(Constant.ERROR_NOT_FOUND_USER, "false");

		}
	}

	public void loadDetailTweet() {
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("tweetid", Integer
				.toString(tweetid)));

		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(Constant.GETTWEET, postParameters);
		result = result.trim();
		if (result.length() > 2) {
			try {
				JSONObject object = new JSONObject(result);

				int tweetid = object.getInt("id");
				String urlImagine = object.getString("avatar");
				String text = object.getString("tweetText");
				String userName = object.getString("username");
				String fullName = object.getString("screenName");
				String time = object.getString("createdTime");
				String url_image = object.getString("url_images");
				int total_retweet = object.getInt("totalRetweet");
				int total_favorite = object.getInt("totalFavorite");
				JSONObject mention_object = object.getJSONObject("mention");
				for (int i = 0; i < mention_object.length(); i++) {
					String men = mention_object.getString(Integer.toString(i));
					if (men.length() > 0) {
						mention_array.add(men);
						mention_string = mention_string + " @" + men;
					}
				}
				tweet = new Tweet(tweetid, urlImagine, text, userName,
						fullName, 0);
				if (url_image.length() > 0)
					tweet.setPost_url_images(url_image);
				if (nameRetweet.length() > 0)
					tweet.setNameRweet(nameRetweet);
				tweet.setIsMyFavorite(isMyFavorite);
				tweet.setIsMyRetweet(isMyRetweet);
				tweet.setTime(time);
				tweet.setTotalRetweet(total_retweet);
				tweet.setTotalFavorite(total_favorite);

				// Log.i(ParseJSON.class.getName(),
				// jsonObject.getString("text"));
				// }
			} catch (Exception e) {
				// e.printStackTrace();
				sharePre.setExitInt("isErrorTweet", tweetid);
				finish();
			}
		} else {
			isError = true;
			sharePre.setExitInt("isErrorTweet", tweetid);
			finish();
		}
	}

	public void showTweet() {
		if (isError)
			return;
		ImageView image = (ImageView) findViewById(R.id.image_profile);

		imageLoader.DisplayImage(tweet.getImageUrl(), image);

		TextView fullname = (TextView) findViewById(R.id.tweet_fullname);
		fullname.setText(tweet.getFullName());

		TextView username = (TextView) findViewById(R.id.tweet_username);
		username.setText(" @" + tweet.getUserName());

		TextView text = (TextView) findViewById(R.id.tweet_dt_text);
		text.setText(tweet.getTweetText());

		RelativeLayout profile_layout = (RelativeLayout) findViewById(R.id.tweet_dt_profile);
		profile_layout.setOnClickListener(this);

		Linkify.addLinks(text, Linkify.WEB_URLS);
		Linkify.addLinks(text, Linkify.EMAIL_ADDRESSES);
		// TextView textView = (TextView) findViewById(R.id.text);
		// text.setText("This is a demo of custom text linkify, @khoa developed by@Bharat");

		MatchFilter matchFilter = new MatchFilter() {
			public final boolean acceptMatch(CharSequence s, int start, int end) {
				// you can compare match over here
				// return s.toString().equals("@Bharat");
				return true;
			}
		};
		TransformFilter transformFilter = new TransformFilter() {
			public final String transformUrl(final Matcher match, String url) {
				return match.group();
			}
		};
		if (tweet.getPost_url_images().length() > 0) {
			Pattern urlMatcher = Pattern.compile(tweet.getPost_url_images());
			String urlScheme = "net.hhk.twitter.viewimage://";
			Linkify.addLinks(text, urlMatcher, urlScheme);
			final ImageView tweet_image = (ImageView) findViewById(R.id.tweet_dt_image);
			tweet_image.setVisibility(View.VISIBLE);
			final ThreadedRequest tReq = new ThreadedRequest();
			tReq.start(new Runnable() {
				public void run() {
					// This would stop whatever the other method
					// started
					// and let the user know
					Object content = null;
					try {
						URL url = new URL(Constant.HOST
								+ tweet.getPost_url_images());
						content = url.getContent();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					InputStream is = (InputStream) content;
					Drawable imagine = Drawable.createFromStream(is, "src");
					// Image01.setImageDrawable(image);
					tweet_image.setImageDrawable(imagine);

				}
			});
		}

		// Pattern pattern = Pattern.compile("@Bharat");
		Pattern userMatcher = Pattern.compile("\\B@[^:\\s]+");
		String scheme = "net.hhk.twitter.userprofile://";
		Linkify.addLinks(text, userMatcher, scheme);

		Pattern hashtagPattern = Pattern.compile("#([A-Za-z0-9_-]+)");
		String hashtagScheme = "http://www.twitter.com/search/";
		Linkify.addLinks(text, hashtagPattern, hashtagScheme, null,
				transformFilter);

		Pattern urlPattern = Patterns.WEB_URL;
		Linkify.addLinks(text, urlPattern, null, null, transformFilter);
		stripUnderlines(text);

		TextView time = (TextView) findViewById(R.id.tweet_dt_time);
		time.setText(tweet.getTime());

		if (nameRetweet.length() > 0) {
			LinearLayout layout = (LinearLayout) findViewById(R.id.tweet_dt_retweet_layout);
			layout.setVisibility(View.VISIBLE);
			TextView retweet = (TextView) findViewById(R.id.tweet_dt_retweet);
			retweet.setText("Retweeted by " + nameRetweet);
		}
		if (isMyFavorite) {
			Button favor = (Button) findViewById(R.id.tweet_dt_favor);
			favor.setBackgroundResource(R.drawable.ic_action_fave_on_default);
		}
		if (isMyRetweet) {
			Button rt = (Button) findViewById(R.id.tweet_dt_retweet_button);
			rt.setBackgroundResource(R.drawable.ic_action_rt_on_default);
		}
		if (tweet.getTotalRetweet() > 0) {
			RelativeLayout retweet_layout = (RelativeLayout) findViewById(R.id.tweet_dt_retweets_layout);
			retweet_layout.setVisibility(View.VISIBLE);
			retweet_layout.setOnClickListener(this);
			TextView number_retweet = (TextView) findViewById(R.id.tweet_dt_numberRetweet);
			number_retweet.setText(Integer.toString(tweet.getTotalRetweet()));
		}
		if (tweet.getTotalFavorite() > 0) {
			RelativeLayout favorite_layout = (RelativeLayout) findViewById(R.id.tweet_dt_favorites_layout);
			favorite_layout.setVisibility(View.VISIBLE);
			favorite_layout.setOnClickListener(this);
			TextView number_favorite = (TextView) findViewById(R.id.tweet_dt_numberFavorite);
			number_favorite.setText(Integer.toString(tweet.getTotalFavorite()));
		}

		// scroll.add
		// scroll.seto
		scroll.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				return;
			}
		});
		loadData(true);
		loadData(false);

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub

		super.onPause();
		int tweetid = tweet.getTweetId();
		String favorite = sharePre.getExitString("favoriteChange");
		String unfavorite = sharePre.getExitString("unfavoriteChange");
		String retweet = sharePre.getExitString("retweetChange");
		String unretweet = sharePre.getExitString("unretweetChange");
		if (currentMyFavorite) {
			if (!isMyFavorite) {
				favorite();
				// sharePre.setExitInt("isFavoriteChange", 1);
				// sharePre.setExitInt("ItemChanged", tweet.getTweetId());

				if (favorite == null)
					favorite = "";
				if (favorite.length() == 0)
					favorite = ";";
				favorite = favorite + Integer.toString(tweetid) + ";";
				if (unfavorite.length() > 0)
					unfavorite = unfavorite.replace(
							";" + Integer.toString(tweetid) + ";", ";");
			}
		} else {
			if (isMyFavorite) {
				unFovorite();
				if (unfavorite == null)
					unfavorite = "";
				if (unfavorite.length() == 0)
					unfavorite = ";";
				unfavorite = unfavorite + Integer.toString(tweetid) + ";";
				if (favorite.length() > 0)
					favorite = favorite.replace(";" + Integer.toString(tweetid)
							+ ";", ";");

			}
		}
		if (currentMyRetweet) {
			if (!isMyRetweet) {
				retweet();
				// sharePre.setExitInt("isRetweetChange", 1);
				// sharePre.setExitInt("ItemChanged", tweet.getTweetId());
				if (retweet == null)
					retweet = "";
				if (retweet.length() == 0)
					retweet = ";";
				retweet = retweet + Integer.toString(tweetid) + ";";
				if (unretweet.length() > 0)
					unretweet = unretweet.replace(
							";" + Integer.toString(tweetid) + ";", ";");
			}
		} else {
			if (isMyRetweet) {
				unRetweet();
				// sharePre.setExitInt("isRetweetChange", 2);
				// sharePre.setExitInt("ItemChanged", tweet.getTweetId());
				if (unretweet == null)
					unretweet = "";
				if (unretweet.length() == 0)
					unretweet = ";";
				unretweet = unretweet + Integer.toString(tweetid) + ";";
				if (retweet.length() > 0)
					retweet = retweet.replace(";" + Integer.toString(tweetid)
							+ ";", ";");
			}
		}
		sharePre.setExitString("favoriteChange", favorite);
		sharePre.setExitString("unfavoriteChange", unfavorite);
		sharePre.setExitString("retweetChange", retweet);
		sharePre.setExitString("unretweetChange", unretweet);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tweet_dt_favor:
			if (currentMyFavorite) {
				currentMyFavorite = false;
				favor.setBackgroundResource(R.drawable.ic_action_fave_off_default);

			} else {
				currentMyFavorite = true;
				favor.setBackgroundResource(R.drawable.ic_action_fave_on_default);

			}
			break;
		case R.id.tweet_dt_reply:
			sharePre.setExitString(Constant.VISIT_USER_NAME,
					tweet.getUserName());
			Intent i = new Intent(getApplicationContext(),
					WriteTweetActivity.class);
			Bundle b = new Bundle();

			b.putInt("isReply", tweet.getTweetId());
			b.putString("mention", mention_string);
			b.putString("quote", "");
			sharePre.setExitString("currentVisitUsername", tweet.getUserName());

			// Your id
			i.putExtras(b);
			startActivity(i);
			break;
		case R.id.tweet_dt_retweet_button:
			String title;
			String retweet_status;
			if (!currentMyRetweet) {
				title = "Are you sure want to retweet this tweet";
				retweet_status ="Retweet";
			}
			else {
				title = "Are you sure want to unretweet this tweet";
				retweet_status ="UnRetweet";
			}

			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
					context);

			alertDialogBuilder
					.setMessage(title)
					.setCancelable(false)
					.setPositiveButton(retweet_status,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									if (currentMyRetweet) {
										currentMyRetweet = false;
										rt.setBackgroundResource(R.drawable.ic_action_rt_off_default);
									} else {
										currentMyRetweet = true;
										rt.setBackgroundResource(R.drawable.ic_action_rt_on_default);
									}
								}
							})
					.setNeutralButton("Quote",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									sharePre.setExitString(
											Constant.VISIT_USER_NAME,
											tweet.getUserName());
									Intent i = new Intent(
											getApplicationContext(),
											WriteTweetActivity.class);
									Bundle b = new Bundle();

									b.putInt("isReply", 0);
									b.putString("mention", "");
									b.putString("quote", tweet.getTweetText());
									sharePre.setExitString(
											"currentVisitUsername",
											tweet.getUserName());

									// Your id
									i.putExtras(b);
									startActivity(i);
								}
							})
					.setNegativeButton("Cancel",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									// if this button is clicked, just
									// close
									// the dialog box and do nothing
									dialog.cancel();
								}
							});

			// create alert dialog
			AlertDialog alertDialog = alertDialogBuilder.create();

			// show it
			alertDialog.show();

			break;
		case R.id.tweet_dt_delete:

			break;
		case R.id.tweet_dt_profile:
			Intent toUserProfile = new Intent(getApplicationContext(),
					UserProfileActivity.class);
			Bundle toBundle = new Bundle();
			toBundle.putInt("id", 0);

			toUserProfile.putExtras(toBundle); // Put your id to your next Inten
			startActivity(toUserProfile);
			sharePre.setExitString("currentVisitUsername", tweet.getUserName());
			break;
		case R.id.tweet_dt_favorites_layout:
			Intent favoriteUser = new Intent(getApplicationContext(),
					UserFavorRetweetActivity.class);
			Bundle favorite = new Bundle();
			favorite.putInt("id", tweet.getTweetId());
			favorite.putBoolean("isFavorite", true);
			favoriteUser.putExtras(favorite);
			startActivity(favoriteUser);
			break;
		case R.id.tweet_dt_retweets_layout:
			Intent retweetUser = new Intent(getApplicationContext(),
					UserFavorRetweetActivity.class);
			Bundle retweet = new Bundle();
			retweet.putInt("id", tweet.getTweetId());
			retweet.putBoolean("isFavorite", false);
			retweetUser.putExtras(retweet);
			startActivity(retweetUser);
			break;

		default:
			break;
		}
	}

	public void deleteTweet() {
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		postParameters.add(new BasicNameValuePair("tweetid", Integer
				.toString(tweet.getTweetId())));
		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(Constant.DELETE_TWEET, postParameters);
		sharePre.setExitInt("isDeleteTweet", tweet.getTweetId());
		finish();
	}

	public void favorite() {
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		postParameters.add(new BasicNameValuePair("tweetid", Integer
				.toString(tweet.getTweetId())));
		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(Constant.POST_FAVORITE, postParameters);

	}

	public void unFovorite() {
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		postParameters.add(new BasicNameValuePair("tweetid", Integer
				.toString(tweet.getTweetId())));
		HTTPConnect http = HTTPConnect.getConnect();
		String result = http
				.doConnect(Constant.POST_UNFAVORITE, postParameters);

	}

	public void retweet() {

		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		postParameters.add(new BasicNameValuePair("tweetid", Integer
				.toString(tweet.getTweetId())));
		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(Constant.POST_RETWEET, postParameters);
	}

	public void unRetweet() {
		List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

		postParameters.add(new BasicNameValuePair("tweetid", Integer
				.toString(tweet.getTweetId())));
		HTTPConnect http = HTTPConnect.getConnect();
		String result = http.doConnect(Constant.POST_UNRETWEET, postParameters);
	}

	private void stripUnderlines(TextView textView) {
		Spannable s = null;
		try {
			s = (Spannable) textView.getText();

		} catch (Exception e) {
			// TODO: handle exception
			return;
		}

		URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
		for (URLSpan span : spans) {
			int start = s.getSpanStart(span);
			int end = s.getSpanEnd(span);
			s.removeSpan(span);
			span = new URLSpanNoUnderline(span.getURL());
			s.setSpan(span, start, end, 0);
		}
		textView.setText(s);
	}

	private class URLSpanNoUnderline extends URLSpan {
		public URLSpanNoUnderline(String url) {
			super(url);
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			super.updateDrawState(ds);
			ds.setUnderlineText(false);
		}
	}

	public void loadReplyTweet(List<Tweet> reply_manager) {
		if (reply_manager.size() > 0) {
			LinearLayout replyLayout = (LinearLayout) findViewById(R.id.replies_text);
			replyLayout.setVisibility(View.VISIBLE);
			for (int i = 0; i < reply_manager.size(); i++) {
				final Tweet twit = reply_manager.get(i);
				LinearLayout list = (LinearLayout) findViewById(R.id.reply_list);
				list.addView(loadView(twit));

			}
		}

	}

	public void loadReplyFrom(List<Tweet> reply_manager) {
		if (reply_manager.size() > 0) {

			LinearLayout replyFromLayout = (LinearLayout) findViewById(R.id.inReplyTo);
			replyFromLayout.setVisibility(View.VISIBLE);

			for (int i = 0; i < reply_manager.size(); i++) {

				final Tweet twit = reply_manager.get(i);
				if (isFirstLoadReply) {
					isFirstLoadReply = false;
					TextView inReplyTo = (TextView) findViewById(R.id.replyTo);
					inReplyTo.setText("In reply to " + twit.getFullName());
				}
				LinearLayout list = (LinearLayout) findViewById(R.id.reply_from_list);
				list.addView(loadView(twit));
				scroll.post(new Runnable() {
					public void run() {
						// TODO Auto-generated method stub
						scroll.scrollTo(0, lo.getTop());
					}
				});
			}

		}
	}

	public View loadView(Tweet twett) {
		final Tweet twit = twett;
		View row = inflater.inflate(R.layout.tweet_layout, null);
		final TextView profileName = (TextView) row
				.findViewById(R.id.profileName);
		profileName.setText(twit.getFullName());
		final TextView username = (TextView) row.findViewById(R.id.userName);
		username.setText(" @" + twit.getUserName());
		ImageView image = (ImageView) row.findViewById(R.id.profileImage);

		imageLoader.DisplayImage(twit.getImageUrl(), image);

		final TextView twitMessage = (TextView) row
				.findViewById(R.id.twitMessage);
		twitMessage.setText(twit.getTweetText());
		int isRetweet = twit.getIsRetweet();
		if (isRetweet > 0) {
			final ImageView retweet_image = (ImageView) row
					.findViewById(R.id.image_retweet);
			retweet_image.setVisibility(View.VISIBLE);
			final TextView retweet_user = (TextView) row
					.findViewById(R.id.name_user_retweet);
			retweet_user.setText("  Retweeted by " + twit.getNameRweet());
		}
		final ImageView conver_picture = (ImageView) row
				.findViewById(R.id.conver_picture);
		if (twit.getPost_url_images().length() > 0) {
			conver_picture.setVisibility(View.VISIBLE);
			conver_picture
					.setImageResource(R.drawable.ic_tweet_attr_photo_default);
		} else if (twit.getIsReply() > 0) {
			conver_picture.setVisibility(View.VISIBLE);
			conver_picture
					.setImageResource(R.drawable.ic_tweet_attr_convo_default);
		} else
			conver_picture.setVisibility(View.GONE);
		final ImageView favorRetweet = (ImageView) row
				.findViewById(R.id.favor_retweet);
		if (twit.getIsMyFavorite() && twit.getIsMyRetweet()) {
			favorRetweet.setVisibility(View.VISIBLE);
			favorRetweet.setBackgroundResource(R.drawable.ic_dogear_both);
		} else if (twit.getIsMyFavorite()) {
			favorRetweet.setVisibility(View.VISIBLE);
			favorRetweet.setBackgroundResource(R.drawable.ic_dogear_fave);
		} else if (twit.getIsMyRetweet()) {
			favorRetweet.setVisibility(View.VISIBLE);
			favorRetweet.setBackgroundResource(R.drawable.ic_dogear_rt);
		} else
			favorRetweet.setVisibility(View.GONE);
		row.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(),
						ViewTweetActivity.class);
				Bundle b = new Bundle();
				b.putInt("id", twit.getTweetId());
				b.putString("retweetUser", twit.getNameRweet());
				b.putBoolean("isfavorite", twit.getIsMyFavorite());
				b.putBoolean("isretweet", twit.getIsMyRetweet());
				sharePre.setExitString("currentVisitUsername",
						twit.getUserName());
				i.putExtras(b); // Put your id to your next Inten
				sharePre.setExitString("currentVisitUsername",
						twit.getUserName());
				startActivity(i);

			}
		});
		return row;
	}

	public void loadData(Boolean isloadReply) {
		final Boolean status = isloadReply;
		AsyncTask<String, Void, List<Tweet>> asyncTask = new AsyncTask<String, Void, List<Tweet>>() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.os.AsyncTask#onPreExecute()
			 */
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
			}

			@Override
			protected List<Tweet> doInBackground(String... params) {
				if (status) {
					return replyManager.getTweetReply(Constant.GET_REPLY_TWEET,
							tweet.getTweetId());
				} else
					return replyFromManager.getTweetReply(
							Constant.GET_REPLY_FROM_TWEET, tweet.getTweetId());
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
			 */
			@Override
			protected void onPostExecute(List<Tweet> result) {
				super.onPostExecute(result);
				if (status)
					loadReplyTweet(result);
				else
					loadReplyFrom(result);

				// scroll.findViewById(R.id.lo_main_content).;
			}
		};
		asyncTask.execute("Saurabh");

	}
}
